package com.csun.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csun.dao.BaseDao;
import com.csun.entity.PageBean;
import com.csun.entity.Role;
import com.csun.entity.User;
import com.csun.service.UserService;
import com.csun.util.StrUtil;

/**
 * @ClassName: UserServiceImpl.java
 * @Description:用户服务接口
 * @author danjinsong danjingsong@dragonwake.cn
 * @date 2017年2月16日下午4:04:39
 * 
 */
@Service("userService")
public class UserServiceImpl implements UserService{

	@Autowired
	private BaseDao<User> userDao;
	
	@Autowired
	private BaseDao<Role> roleDao;
	
	
	@Override
	public User login(User user) {
		String hql = " from User u where u.userName = '"+user.getUserName()+"' ";
		User u = userDao.get(hql,new Object[]{});
		if(u.getPassword().equals(StrUtil.MD5(user.getPassword().trim()))){
			return u ;
		}
		return null;
	}

	@Override
	public void save(User user) {
		userDao.saveOrUpdate(user);
	}

	@Override
	public void delete(User user) {
		userDao.delete(user);
	}
	
	@Override
	public void delete(Integer id) {
		User user = new User();
		user.setUserId(id);
		userDao.delete(user);
	}
	
	@Override
	public User get(User s_user) {
		StringBuffer hql = new StringBuffer(" from User u ");
		if(s_user != null){
			if(StrUtil.isNotEmpty(s_user.getFirstName())){
				hql.append(" and u.firstName like '%"+s_user.getFirstName()+"%'");
			}else if(StrUtil.isNotEmpty(s_user.getLastName())){
				hql.append(" and u.lastName like '%"+s_user.getLastName()+"%'");
			}else if(s_user.getUserType() != null){
				hql.append(" and u.userType = "+s_user.getUserType()+" ");
			}		
		}		
		return userDao.get(hql.toString().replaceFirst(" and ", " where "), new Object[]{});
	}

	@Override
	public List<User> find(User s_user, PageBean pageBean) {
		StringBuffer hql = new StringBuffer(" from User u ");
		if(s_user != null){
			if(StrUtil.isNotEmpty(s_user.getFirstName())){
				hql.append(" and u.firstName like '%"+s_user.getFirstName()+"%'");
			}else if(StrUtil.isNotEmpty(s_user.getLastName())){
				hql.append(" and u.lastName like '%"+s_user.getLastName()+"%'");
			}else if(s_user.getUserType() != null){
				hql.append(" and u.userType = "+s_user.getUserType()+" ");
			}
			//根据id判断倒序逆序
			if(s_user.getUserId() != null){
				hql.append(" order by u.userId desc ");
			}
		}
		if(pageBean != null)
			return userDao.find(hql.toString().replaceFirst(" and ", " where "), new Object[]{}, pageBean);
		return userDao.find(hql.toString().replaceFirst(" and ", " where "), new Object[]{});
	}

	@Override
	public Long count(User s_user) {
		StringBuffer hql = new StringBuffer("select count(*) as total from User u ");
		if(s_user != null){
			if(StrUtil.isNotEmpty(s_user.getFirstName())){
				hql.append(" and u.firstName like '%"+s_user.getFirstName()+"%'");
			}else if(StrUtil.isNotEmpty(s_user.getLastName())){
				hql.append(" and u.lastName like '%"+s_user.getLastName()+"%'");
			}else if(s_user.getUserType() != null){
				hql.append(" and u.userType = "+s_user.getUserType()+" ");
			}
		}
		return userDao.count(hql.toString().replaceFirst(" and ", " where "));
	}

	@Override
	public User get(Integer id) {
		return userDao.get(User.class, id);
	}

	@Override
	public void save(User user, Integer roleId) {
		Short userType = 2;									//默认服务者
		if(roleId == 1){
			userType = 1;									//管理者
		}else if(roleId == 3){	
			userType = 3;									//老人
		}else if(roleId == 4){
			userType = 4;									//家属
		}
		if(StrUtil.isEmpty(user.getPassword())){			//设置默认密码
			user.setPassword("123456");
		}
		Role role = roleDao.get(Role.class, roleId);		//查找role
		user.setRole(role); 								//建立映射关系													
		
		user.setPassword(StrUtil.MD5(user.getPassword()));	//加密
		user.setUserType(userType); 						//设置用户类型	
		userDao.saveOrUpdate(user);  						//保存用户 底层自动级联保存
		
	}

	@Override
	public Boolean isExit(String userName) {
		String hql = new String("from User u where u.userName = ?");
		return (userDao.get(hql, new Object[]{userName})==null?false:true);
	}

	@Override
	public void delete(String ids) {
		String sql = "delete from t_user where userId in ("+ids+")";
		userDao.executeSql(sql);
	}
}
